import 'dart:core';
// import 'dart:html';

import 'package:appinio_video_player/appinio_video_player.dart';
import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

import '../../controller/homeTabController.dart';

class VideoPlayerModel extends ChangeNotifier{
  bool isAutoPlay = true;
  int currentIndex = 0;
  bool isAgree = true;
  final FijkPlayer player = FijkPlayer();

  List<VideoPlayerController> videoPlayerControllers = []; // 创建一个视频播放器列表
  VideoPlayerModel(List<String> videoUrls,bool isAgree) {
    this.isAgree = isAgree;
    initVideoPlayer1(videoUrls);
  }
  Future<void> initVideoPlayer1(List<String> videoUrls) async {


    if(videoPlayerControllers.length > 0) {
      await videoPlayerControllers[0].dispose();
      // player.release();

    }

    videoPlayerControllers.clear(); // 清除之前的视频播放器
    for (int i = 0; i < videoUrls.length; i++) {
      VideoPlayerController videoPlayerController = VideoPlayerController.network(
        // 'http://209.209.10.156:9000/upload/3/4/3475c2cc02452591f39d1d2cc4c2f437/3475c2cc02452591f39d1d2cc4c2f437.m3u8?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20250118%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250118T012332Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=6797e5f02947bc6b9fb3b5c25ca4ec80f6f021ebe0bfeecf31ae56acae7dc16c',
            videoUrls[i],
          videoPlayerOptions: VideoPlayerOptions(
            mixWithOthers: true,
              allowBackgroundPlayback: true,

          )


    );
      videoPlayerControllers.add(videoPlayerController); // 将视频播放器添加到列表中
      await videoPlayerController.initialize();
      if(i == 0 && isAgree) {

        videoPlayerController.play();
        currentIndex = i;
      }

      videoPlayerController.setLooping(true);

      // 创建BetterPlayer控制器


      notifyListeners();

    }
    // player.setDataSource(videoUrls[0]);
    notifyListeners();
  }

  void disposeVideoPlayers() {
    for (VideoPlayerController controller in videoPlayerControllers) {
      controller.dispose(); // 释放每个视频播放器的资源
    }
    videoPlayerControllers.clear(); // 清除视频播放器列表

    // player.release();
    notifyListeners();
  }

  void pasueVideoPlayer(int index) {

    videoPlayerControllers[currentIndex].pause();
    // videoPlayerControllers[index].play();
    currentIndex = index;
    notifyListeners();
  }

}